import axios from '@/utils/request'
const _ = require('lodash')
export function deployment (parameter) {
  return axios({
    url: '/bpmn/deployment/create',
    headers: {
      'req_type': 'bpmn',
      'user_guid': JSON.parse(sessionStorage.getItem('user')).account
    },
    method: 'post',
    data: parameter
  })
}

export function deploy (data) {
  return axios({
    url: '/api/bpm/deploy',
    method: 'post',
    data: data
  })
}

export function getProcessDefinitionStatistics () {
  return axios({
    url: '/api/bpm/rest/process-definition/statistics?incidents=true',
    method: 'get'
  })
}

export function deleteProcessDefinition (id) {
  const option = {
    'process-definition-id': id,
    'cascade': true,
    'skipCustomListeners': true,
    'skipIoMappings': true
  }
  return axios({
    url: '/api/bpm/process-definition/delete',
    method: 'post',
    data: option
  })
}

export function getStartFormKey (id) {
  return axios({
    url: `/rest/process-definition/key/${id}/startForm`,
    headers: {
      'req_type': 'bpmn',
      'user_guid': JSON.parse(sessionStorage.getItem('user')).account
    },
    method: 'get'
  })
}

export function getTaskFormKey (data) {
  return axios({
    url: '/api/bpm/task/form',
    method: 'post',
    data: data
  })
}

export function setTaskAssignee (data) {
  return axios({
    url: '/api/bpm/task/assignee',
    method: 'post',
    data: data
  })
}
export function addTaskIdentityLinks (data) {
  return axios({
    url: '/api/bpm/task/identity-links',
    method: 'post',
    data: data
  })
}

export function delProcessInst (id) {
  return axios({
    url: `/rest/process-instance/${id}`,
    method: 'delete',
    headers: {
      'req_type': 'bpmn',
      'user_guid': JSON.parse(sessionStorage.getItem('user')).account
    }
  })
}

export function suspendedProcessInst (data) {
  return axios({
    url: '/api/bpm/process-instance/suspended',
    method: 'post',
    data: data
  })
}

export function delTaskIdentityLinks (data) {
  return axios({
    url: '/api/bpm/task/identity-links/delete',
    method: 'post',
    data: data
  })
}

export function getProcessDefinitionKeyXml (id) {
  return axios({
    url: `/rest/process-definition/key/${id}/xml`,
    headers: {
      'req_type': 'bpmn',
      'user_guid': JSON.parse(sessionStorage.getItem('user')).account
    },
    method: 'get'
  })
}


export function getProcessDefinitionXml (id) {
  return axios({
    url: `/rest/process-definition/${id}/xml`,
    headers: {
      'req_type': 'bpmn',
      'user_guid': JSON.parse(sessionStorage.getItem('user')).account
    },
    method: 'get'
  })
}

export function getTaskGroupBy (processDefinitionId) {
  return new Promise((resolve, reject) => {
    axios({
      url: `/api/bpm/rest/task?processDefinitionId=${processDefinitionId}`,
      method: 'get'
    }).then((res) => {
      const groupBy = _.groupBy(res, (item) => {
        return item.taskDefinitionKey
      })
      const result = []
      _.forEach(groupBy, (value, key) => {
        result.push({
          key: key,
          value: value
        })
      })
      resolve(result)
    }).catch(function (error) {
      reject(error)
    })
  })
}

export function startProcess (data) {
  return axios({
    url: `/bpmn/process-definition/key/${data.key}/submit-form`,
    headers: {
      'req_type': 'bpmn',
      'user_guid':JSON.parse(sessionStorage.getItem('user')).account
    },
    method: 'post',
    data: data.form
  })
}

export function setAssignee (id, data) {
  return axios({
    url: `/api/bpm/rest/task/${id}/assignee`,
    method: 'post',
    data: data
  })
}

export function submitProcess (data) {
  return axios({
    url: `/bpmn/task/${data.taskId}/submit-form`,
    headers: {
      'req_type': 'bpmn',
      'user_guid':JSON.parse(sessionStorage.getItem('user')).account
    },
    method: 'post',
    data: data.form
  })
}

export function getMyApply (data) {
  return axios({
    url: '/api/bpm/business/my-apply',
    method: 'post',
    data: data
  })
}

export function getMyTodo (data) {
  return axios({
    url: '/api/bpm/business/my-to-do',
    method: 'post',
    data: data
  })
}

export function getMyDone (data) {
  return axios({
    url: '/api/bpm/business/my-done',
    method: 'post',
    data: data
  })
}

/*
  我的申请列表
 */
export function getMyApplyList (data) {
  return axios({
    url:  '/bpmn/process/getMyApplyList',
    method: 'post',
    data: data
  })
}

/*
  待审核列表
 */
export function getTobeReviewedList (data) {
  return axios({
    url: '/bpmn/process/getTobeReviewedList',
    method: 'post',
    data: data
  })
}

/*
  已审核列表
 */
export function getMyReviewedList (data) {
  return axios({
    url: '/bpmn/process/getMyReviewedList',
    method: 'post',
    data: data
  })
}

/*
  已审核列表
 */
export function getTaskList (data) {
  return axios({
    url: '/guns/bpmn/getTaskList',
    method: 'post',
    data: data
  })
}

/*
  查询用户列表
 */
export function getUserList (data) {
  return axios({
    url: '/bpmn/process/getUserList',
    method: 'post',
    data: data
  })
}

/*
  查询岗位列表
 */
export function getRoleList (data) {
  return axios({
    url: '/bpmn/process/getRoleList',
    method: 'post',
    data: data
  })
}

/*
  获取审批历史表单数据
 */
export function getTaskFormData (id) {
  return axios({
    headers: {
      'req_type': 'bpmn'
    },
    url: `/rest/task/${id}/form-variables`,
    method: 'get'
  })
}

/*
  删除流程分类
 */
export function deleteCategoryByID (data) {
  return axios({
    url: '/bpmn/process/deleteCategoryByID',
    method: 'post',
    data: data
  })
}

/*
  查询流程配置列表
 */
export function getProcessList (data) {
  return axios({
    url: '/bpmn/process/getProcessList',
    method: 'post',
    data: data
  })
}
// 设置回显
export function setTableShowColumn (data) {
  return axios({
    url: '/bpmn/process/setTableShowColumn',
    method: 'post',
    data: data
  })
}
// 获取回显
export function getTableConfig (data) {
  return axios({
    url: '/bpmn/process/getTableConfig',
    method: 'post',
    data: data
  })
}

// 查询表数据
export function getTableData (data) {
  return axios({
    url: '/bpmn/process/getTableData',
    method: 'post',
    data: data
  })
}

/*
  查询流程配置列表
 */
export function getSelectUserList (data) {
  return axios({
    url: '/bpmn/process/getSelectUserList',
    method: 'post',
    data: data
  })
}

/*
  查询流程配置列表
 */
export function deleteProcessByID (data) {
  return axios({
    url: '/bpmn/process/deleteProcessByID',
    method: 'post',
    data: data
  })
}/*
  查询流程配置列表
 */
export function updateWarnStatus (data) {
  return axios({
    url: '/bpmn/process/updateWarnStatus',
    method: 'post',
    data: data
  })
}

export const showTableColumn = async data =>
  await axios
    .post('/bpmn/process/showTableColumn' , data)
    .then(res => res)
